package com.hust360.percent.local_db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class LocalDBHelper extends SQLiteOpenHelper {

	public LocalDBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		/**
		 * 店铺表
		 */
		db.execSQL("CREATE TABLE shop(" 
				+ "id INTEGER PRIMARY KEY," //店铺的id
				+ "name VARCHAR(50) NOT NULL," //店铺的名字
				+ "subname VARCHAR(50),"
				+ "brandid INTEGER DEFAULT '-1', " //牌子-1代表没有
				+ "address VARCHAR(50) NOT NULL," //店铺的地址 
				+ "areaid INTEGER NOT NULL DEFAULT '-1'," //店铺所在的商场，-1代表无
				
				//+ "totalRate REAL NOT NULL DEFAULT '0'," //
				
				+ "telephone VARCHAR(30) DEFAULT '未录入', " //电话
				+ "logoimg VARCHAR(50) NOT NULL, " //logo图片的名字
				+ "logoURL VARCHAR(100) NOT NULL, " //图片的url 
									 /* ********* */
				+ "longitude REAL, " /*	店铺的经纬度*/
				+ "latitude REAL," 	 /*	********* */
				
				+ "cityid INTEGER NOT NULL," //所在的城市的id
				+ "type INT NOT NULL DEFAULT '0', " //店铺的类别
				+ "moddate DATETIME NOT NULL DEFAULT (datetime('now','localtime'))"
				// id最大的打折信息就是当前激活的id
				// + "curdsc" + " LONG" //当前激活的
				+ ")");

		/**
		 * 打折信息表
		 */
		db.execSQL("CREATE TABLE discount( "
				+ "id INTEGER PRIMARY KEY," //打折信息的id
				+ "shopid INTEGER NOT NULL," //店铺id
				
				// 可以根据根据计算获取
				//+ "rate REAL NOT NULL DEFAULT '0', " //打折信息的总评分
				
				+ "describe VARCHAR(200) NOT NULL DEFAULT '商家没有提供说明'," //打折信息的说明 
				+ "disctype INT NOT NULL DEFAULT '1'," //打折类别 全部 or 部分
				+ "discount INT NOT NULL DEFAULT '0'," // 折扣
				
				//   从图片和打折关系表中获取
				//+ "discountImage SMALL TEXT," // 打折信息对应的图片 
				
				//+ "liked INTEGER NOT NULL DEFAULT '0'," //收藏的人数
				//+ "type INT NOT NULL DEFAULT '0', "
				
				+ "startdate DATETIME NOT NULL DEFAULT (datetime('now','localtime')),"
				+ "enddate DATETIME NOT NULL DEFAULT (datetime('now', '+3 day','localtime')),"
				+ "moddate DATETIME NOT NULL DEFAULT (datetime('now','localtime'))"
				+ ")");
		
		/**
		 * 图片和打折信息关系表
		 */
		db.execSQL("CREATE TABLE image(" +
				"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
				"dscid INTEGER NOT NULL," +
				"filename VARCHAR(100) NOT NULL," +
				"url VARCHAR(100) NOT NULL" +
				")");
		
		/**
		 * 店铺的收藏的关系表
		 */
		db.execSQL("CREATE TABLE liked(" +
				"id INTEGER PRIMARY KEY AUTOINCREMENT," + //收藏关系id
				"uid INTEGER NOT NULL," + //用户id
				"shopId INTEGER NOT NULL" + //店铺id
				")");
		
		/**
		 * 品牌的收藏关系表 
		 */
		db.execSQL("CREATE TABLE brand_like(" +
				"id INTEGER PRIMARY KEY AUTOINCREMENT," +
				"uid INTEGER NOT NULL," +
				"brandId INTEGER NOT NULL" +
				")");
		

		/**
		 * 区域表
		 */
		db.execSQL("CREATE TABLE Area("
				+ "id INTEGER PRIMARY KEY, " + //区域的id
				"name VARCHAR(255) NOT NULL," //区域的名字
				//+ "shopList SMALL TEXT," //
				+ "img VARCHAR(255), " //区域显示的图片
				+ "title VARCHAR(100) DEFAULT '未录入',"  //显示在区域列表上的文字
				+ "longitude REAL, " //经度
				+ "latitude REAL,"  //纬度
				+ "fromdate DATETIME NOT NULL DEFAULT (datetime('now','localtime'))," //打折开始的时间
				+ "deadline DATETIME DEFAULT (datetime('now','localtime','+3 days'))," //打折结束的时间
				+ "modifydate DATETIME NOT NULL DEFAULT (datetime('now','localtime')), " //修改的时间
				+ "type INT NOT NULL,"  // 1、百货商城 2、家具家装 3、电器数码、4、美食广场、5娱乐天地
				+ "cityid" + " INTEGER NOT NULL" 
				+ ")");
		
		/**
		 * 创建品牌
		 */
		db.execSQL("CREATE TABLE brand(" + 
				"id INTEGER PRIMARY KEY," + //品牌的id
				"name VARCHAR(100) NOT NULL," + //品牌的名字
				"logo VARCHAR(100) NOT NULL" + //品牌的logo
				")");
		
		/**
		 * 用户表 
		 */
		db.execSQL("CREATE TABLE user(" +
				"id INTEGER PRIMARY KEY," + //用户的uid 
				"mail VARCHAR(30) UNIQUE  NOT NULL," + //用户邮箱
				"username VARCHAR(30) UNIQUE NOT NULL," + //用户名 
				"password VARCHAR(50) NOT NULL," + //密码采用加密方式保存
				"img VARCHAR(100)" + //用户的头像 
				")");
		
		/**
		 * 评论的表
		 */
		db.execSQL("CREATE TABLE appraise(" + 
				"id INTEGER PRIMARY KEY," + //评论的id
				"dscid INTEGER NOT NULL," + //评论对应的打折信息的id
				"uid INTEGER NOT NULL," +  // 发出评论的用户的id
				"content MIDDLE TEXT NOT NULL," + // 评论内容
				"rate REAL NOT NULL" + // 用户给打折信息打的分数 
				")");
		
		/**
		 * 足迹的表 		
		 */
		db.execSQL("CREATE TABLE dsc_usr(" +
				"id INTEGER PRIMARY KEY," + // 足迹的id
				"discid INTEGER NOT NULL," + // 对应的打折信息的id
				"uid INTEGER NOT NULL" + //用户的id
				")");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
